package com.caishi.lkx.exam.api;

import cn.hutool.json.JSONObject;
import com.caishi.lkx.common.AdminBaseApi;
import com.caishi.lkx.common.context.RequireContext;
import com.caishi.lkx.common.context.Role;
import com.caishi.lkx.common.role.Roles;
import com.caishi.lkx.exam.model.VersionModel;
import com.caishi.lkx.exam.service.IVersionService;
import com.caishi.lkx.user.service.IGroupService;
import com.zzw.common.Wrappers;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: huangpeng
 * @Date: 2022/9/20 14:47
 * @Description:
 */
@Tag(name = "版本管理相关接口")
@RestController
@RequestMapping("/version")
public class VersionApi extends AdminBaseApi<IVersionService, VersionModel, Long> {
    @Resource
    private IVersionService versionService;
    @Resource
    private IGroupService groupService;

    @Override
    public IVersionService loadService() {
        return versionService;
    }

    @GetMapping("/current")
    @Operation(summary = "查找最新版本")
    @Roles
    @RequireContext(Role.APP)
    public VersionModel current(@ModelAttribute VersionModel model) {
        var groupId = context.getGroupId();
        model.setGroupId(groupId);
        return this.versionService.selectFirst(Wrappers.lambdaQuery(model).orderByDesc(VersionModel::getId));
    }

    @Override
    protected List<VersionModel> listCover(List<VersionModel> result, JSONObject otherParam) {
        result.parallelStream().forEach(e -> e.setGroupName(groupService.getById(e.getGroupId()).getName()));
        return super.listCover(result, otherParam);
    }
}
